package com.example.nirvana.design.singleton;

/**
 * 单例模式-懒汉式（双重校验锁DCL）
 * @decription 比简单懒汉式提高了性能
 */
public class LazyDoubleChechSingleton {
    //防止指令重排序
    private volatile static LazyDoubleChechSingleton instance;

    private LazyDoubleChechSingleton() {

    }

    public LazyDoubleChechSingleton getInstance() {
        if(instance == null) {
            //检查是否要阻塞
            synchronized(LazyDoubleChechSingleton.class) {
                //检查是否要重新创建实例
                if(instance == null) {
                    instance = new LazyDoubleChechSingleton();
                }
            }
        }
        return instance;
    }
}
